package com.example;

import javax.microedition.khronos.opengles.GL10;

import com.step.Step;
import com.step.Step.Dimension;
import com.step.graphic.Texture;
import com.step.mesh.Mesh;
import com.step.sprite.Sprite.RenderType;

public class Mesh3DExample extends BaseExample {
	Texture texture;
	Mesh mesh;
	@Override
	public void create() {
		texture = new Texture("textures/logo.png");
		int vertexNum = 24;
		mesh = new Mesh(Dimension.d3d, vertexNum, 12, true, true, false);
		// 前面
		mesh.addVertex(-0.5f, -0.5f, 0.5f, 255, 0, 0, 255, 0, 1);
		mesh.addVertex( 0.5f, -0.5f, 0.5f, 255, 0, 0, 255, 1, 1);
		mesh.addVertex( 0.5f,  0.5f, 0.5f, 255, 0, 0, 255, 1, 0);
		mesh.addVertex(-0.5f,  0.5f, 0.5f, 255, 0, 0, 255, 0, 0);

		// 右面
		mesh.addVertex(0.5f, -0.5f,  0.5f, 0, 255, 0, 255, 0, 1);
		mesh.addVertex(0.5f, -0.5f, -0.5f, 0, 255, 0, 255, 1, 1);
		mesh.addVertex(0.5f,  0.5f, -0.5f, 0, 255, 0, 255, 1, 0);
		mesh.addVertex(0.5f,  0.5f,  0.5f, 0, 255, 0, 255, 0, 0);

		// 后面
		mesh.addVertex( 0.5f, -0.5f, -0.5f, 0, 0, 255, 255, 0, 1);
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 0, 0, 255, 255, 1, 1);
		mesh.addVertex(-0.5f,  0.5f, -0.5f, 0, 0, 255, 255, 1, 0);
		mesh.addVertex( 0.5f,  0.5f, -0.5f, 0, 0, 255, 255, 0, 0);

		// 左面
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 255, 0, 255, 255, 0, 1);
		mesh.addVertex(-0.5f, -0.5f,  0.5f, 255, 0, 255, 255, 1, 1);
		mesh.addVertex(-0.5f,  0.5f,  0.5f, 255, 0, 255, 255, 1, 0);
		mesh.addVertex(-0.5f,  0.5f, -0.5f, 255, 0, 255, 255, 0, 0);

		//  上面
		mesh.addVertex(-0.5f, 0.5f,  0.5f, 255, 255, 0, 255, 0, 1);
		mesh.addVertex( 0.5f, 0.5f,  0.5f, 255, 255, 0, 255, 1, 1);
		mesh.addVertex( 0.5f, 0.5f, -0.5f, 255, 255, 0, 255, 1, 0);
		mesh.addVertex(-0.5f, 0.5f, -0.5f, 255, 255, 0, 255, 0, 0);

		// 下面
		mesh.addVertex(-0.5f, -0.5f,  0.5f, 0, 255, 255, 255, 0, 1);
		mesh.addVertex( 0.5f, -0.5f,  0.5f, 0, 255, 255, 255, 1, 1);
		mesh.addVertex( 0.5f, -0.5f, -0.5f, 0, 255, 255, 255, 1, 0);
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 0, 255, 255, 255, 0, 0);
		
		// 索引
		mesh.addFace(0, 1, 3);
		mesh.addFace(1, 2, 3);
		mesh.addFace(4, 5, 7);
		mesh.addFace(5, 6, 7);
		mesh.addFace(8, 9, 11);
		mesh.addFace(9, 10, 11);
		mesh.addFace(12, 13, 15);
		mesh.addFace(13, 14, 15);
		mesh.addFace(16, 17, 19);
		mesh.addFace(17, 18, 19);
		mesh.addFace(20, 21, 23);
		mesh.addFace(21, 22, 23);
	}

	float rotation = 0.5f;

	@Override
	public void update() {
		GL10 gl = Step.gl;
		gl.glClearColor(0.5f, 0.5f, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		texture.bind();
		rotation += 0.5f;
		//gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glTranslatef(0, 0, -0.5f);
		gl.glRotatef(rotation, 1, 0, 0);
		gl.glRotatef(rotation, 0, 1, 0);
		gl.glRotatef(rotation, 0, 0, 1);

		mesh.render(gl, RenderType.TRIANGLES);
		gl.glPopMatrix();
		gl.glDisable(GL10.GL_TEXTURE_2D);
	}
}
